<?php

namespace app\admin\controller\hotel;

use app\common\controller\Backend;

class Couponlist extends Backend
{

    // 设置关联查询
    protected $relationSearch = true;

    // 当前模型
    protected $model = null;

    // 无需登陆
    protected $noNeedLogin = [];
    // 无需鉴权
    protected $noNeedRight = [];

    public function __construct(){
        parent::__construct();

        $this->CouponlistModel = model('Hotel.Couponlist');

        $this->BusinessModel = model('Business.User');

        $this->CouponModel = model('Hotel.Coupon');
    }

    // 领取详情
    public function index($ids=NULL){
        // 查询优惠券是否存在
        $coupon = $this->CouponModel->find($ids);
        if(!$coupon){
            $this->error('优惠券信息未知');
            exit;
        }

        $this->request->filter(['strip_tags','trim']);

        if($this->request->isAjax()){
            list($where,$sort,$order,$offset,$limit) = $this->buildparams();

            $total = $this->CouponlistModel
                ->where($where)
                ->where(['cid' => $ids])
                ->order($sort,$order)
                ->count();

            $list = $this->CouponlistModel
                ->with(['coupon','business'])
                ->where($where)
                ->where(['cid' => $ids])
                ->order($sort,$order)
                ->limit($offset,$limit)
                ->select();

            $result = [
                'total' => $total,
                'rows' => $list
            ];

            return json($result);
        }
        return $this->view->fetch();
    }
}
